﻿#region Copyright (c) 2006-2012 nHydrate.org, All Rights Reserved
// --------------------------------------------------------------------- *
//                           NHYDRATE.ORG                                *
//              Copyright (c) 2006-2012 All Rights reserved              *
//                                                                       *
//                                                                       *
// This file and its contents are protected by United States and         *
// International copyright laws.  Unauthorized reproduction and/or       *
// distribution of all or any portion of the code contained herein       *
// is strictly prohibited and will result in severe civil and criminal   *
// penalties.  Any violations of this copyright will be prosecuted       *
// to the fullest extent possible under law.                             *
//                                                                       *
// THE SOURCE CODE CONTAINED HEREIN AND IN RELATED FILES IS PROVIDED     *
// TO THE REGISTERED DEVELOPER FOR THE PURPOSES OF EDUCATION AND         *
// TROUBLESHOOTING. UNDER NO CIRCUMSTANCES MAY ANY PORTION OF THE SOURCE *
// CODE BE DISTRIBUTED, DISCLOSED OR OTHERWISE MADE AVAILABLE TO ANY     *
// THIRD PARTY WITHOUT THE EXPRESS WRITTEN CONSENT OF THE NHYDRATE GROUP *
//                                                                       *
// UNDER NO CIRCUMSTANCES MAY THE SOURCE CODE BE USED IN WHOLE OR IN     *
// PART, AS THE BASIS FOR CREATING A PRODUCT THAT PROVIDES THE SAME, OR  *
// SUBSTANTIALLY THE SAME, FUNCTIONALITY AS THIS PRODUCT                 *
//                                                                       *
// THE REGISTERED DEVELOPER ACKNOWLEDGES THAT THIS SOURCE CODE           *
// CONTAINS VALUABLE AND PROPRIETARY TRADE SECRETS OF NHYDRATE,          *
// THE REGISTERED DEVELOPER AGREES TO EXPEND EVERY EFFORT TO             *
// INSURE ITS CONFIDENTIALITY.                                           *
//                                                                       *
// THE END USER LICENSE AGREEMENT (EULA) ACCOMPANYING THE PRODUCT        *
// PERMITS THE REGISTERED DEVELOPER TO REDISTRIBUTE THE PRODUCT IN       *
// EXECUTABLE FORM ONLY IN SUPPORT OF APPLICATIONS WRITTEN USING         *
// THE PRODUCT.  IT DOES NOT PROVIDE ANY RIGHTS REGARDING THE            *
// SOURCE CODE CONTAINED HEREIN.                                         *
//                                                                       *
// THIS COPYRIGHT NOTICE MAY NOT BE REMOVED FROM THIS FILE.              *
// --------------------------------------------------------------------- *
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using nHydrate.Generator.Common.Util;
using System.ComponentModel;
using DslModeling = global::Microsoft.VisualStudio.Modeling;

namespace nHydrate.Dsl
{
	partial class FunctionParameter : nHydrate.Dsl.IContainerParent
	{
		#region Constructors
		// Constructors were not generated for this class because it had HasCustomConstructor
		// set to true. Please provide the constructors below in a partial class.
		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="store">Store where new element is to be created.</param>
		/// <param name="propertyAssignments">List of domain property id/value pairs to set once the element is created.</param>
		public FunctionParameter(DslModeling::Store store, params DslModeling::PropertyAssignment[] propertyAssignments)
			: this(store != null ? store.DefaultPartitionForClass(DomainClassId) : null, propertyAssignments)
		{
		}

		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="partition">Partition where new element is to be created.</param>
		/// <param name="propertyAssignments">List of domain property id/value pairs to set once the element is created.</param>
		public FunctionParameter(DslModeling::Partition partition, params DslModeling::PropertyAssignment[] propertyAssignments)
			: base(partition, propertyAssignments)
		{
		}
		#endregion

		#region Names
		public string CamelName
		{
			get { return StringHelper.DatabaseNameToCamelCase(this.PascalName); }
		}

		public string DatabaseName
		{
			get { return this.Name; }
		}

		public string PascalName
		{
			get
			{
				if (!string.IsNullOrEmpty(this.CodeFacade))
					return StringHelper.DatabaseNameToPascalCase(this.CodeFacade);
				else
					return StringHelper.DatabaseNameToPascalCase(this.Name);
			}
		}
		#endregion

		#region IContainerParent Members

		DslModeling.ModelElement IContainerParent.ContainerParent
		{
			get { return this.Function; }
		}

		#endregion

	}

	partial class FunctionParameterBase
	{
		partial class NamePropertyHandler
		{
			protected override void OnValueChanged(FunctionParameterBase element, string oldValue, string newValue)
			{
				//If not laoding then parse the name for the data type
				var hasChanged = false;
				if (element.Function != null && !element.Function.nHydrateModel.IsLoading)
				{
					if (!string.IsNullOrEmpty(newValue))
					{
						var arr = newValue.Split(':');
						if (arr.Length == 2)
						{
							var typearr = arr[1].Split(' ');
							var d = Extensions.GetDataTypeFromName(typearr[0]);
							if (d != null)
							{
								if (typearr.Length == 2)
								{
									int len;
									if (int.TryParse(typearr[1], out len))
									{
										element.DataType = d.Value;
										element.Length = len;
										newValue = arr[0];
										hasChanged = true;
									}
									else
									{
										throw new Exception("Unrecognized data type! Valid format is 'Name:Datatype length'");
									}
								}
								else
								{
									element.DataType = d.Value;
									newValue = arr[0];
									hasChanged = true;
								}

							}
							else
							{
								throw new Exception("Unrecognized data type! Valid format is 'Name:Datatype length'");
							}
						}
					}
				}

				base.OnValueChanged(element, oldValue, newValue);

				//Reset after we set datatype
				if (hasChanged)
					element.Name = newValue;
				else
					base.OnValueChanged(element, oldValue, newValue);
			}
		}

		partial class LengthPropertyHandler
		{
			protected override void OnValueChanged(FunctionParameterBase element, int oldValue, int newValue)
			{
				base.OnValueChanged(element, oldValue, newValue);

				//this will trigger another event
				var v = newValue;
				if (v < 0) v = 0;
				v = element.DataType.ValidateDataTypeMax(v);
				if (newValue != v)
					element.Length = element.DataType.ValidateDataTypeMax(v);
			}
		}

		partial class ScalePropertyHandler
		{
			protected override void OnValueChanged(FunctionParameterBase element, int oldValue, int newValue)
			{
				base.OnValueChanged(element, oldValue, newValue);

				//this will trigger another event
				if (newValue < 0) element.Scale = 0;
			}
		}

	}

}
